﻿namespace WMS6.Web
{
    using System;
    using System.IO;
    using System.Runtime.InteropServices;
    using System.Security.Cryptography;
    using System.Text;

    public class EnDeCrypt
    {
        private const int CONST_INC = 11;
        private const string CONST_KEY = "Qt-Asia";
        private int Seed;
        private string SubKey;

        public EnDeCrypt()
        {
            this.SubKey = string.Empty;
            this.Seed = 0;
            this.SubKey = "PowerWMS6";
            this.Seed = this.GenerateSeed();
        }

        public EnDeCrypt(string subKey)
        {
            this.SubKey = string.Empty;
            this.Seed = 0;
            if (string.IsNullOrEmpty(subKey))
            {
                this.SubKey = subKey;
            }
            else
            {
                this.SubKey = "PowerWMS6";
            }
            this.Seed = this.GenerateSeed();
        }

        public string DeCrypt(string sValue)
        {
            StringBuilder deString = new StringBuilder();
            Encoding ascii = Encoding.ASCII;
            Random rand = new Random(this.Seed);
            byte[] bytechr = new byte[1];
            for (int i = 0; i < sValue.Length; i += 2)
            {
                int x = Convert.ToInt32(sValue.Substring(i, 2), 0x10);
                bytechr[0] = Convert.ToByte((int) (x ^ (rand.Next(0xfe) + 1)));
                deString.Append(ascii.GetString(bytechr));
            }
            return deString.ToString();
        }

        public static string DecryptString(string input)
        {
            if (input.Equals(string.Empty))
            {
                return input;
            }
            byte[] byKey = StringToByteArray("Qt3A!sia");
            byte[] IV = StringToByteArray("p*-W?m!s");
            byte[] inputByteArray = new byte[input.Length];
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            inputByteArray = Convert.FromBase64String(input);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            Encoding encoding = new UTF8Encoding();
            return encoding.GetString(ms.ToArray());
        }

        public string EnCrypt(string sValue)
        {
            StringBuilder enString = new StringBuilder();
            Random rand = new Random(this.Seed);
            char[] chars = sValue.ToCharArray();
            for (int i = 0; i < chars.Length; i++)
            {
                int z = chars[i] ^ (rand.Next(0xfe) + 1);
                if (z <= 15)
                {
                    enString.Append("0");
                }
                enString.Append(z.ToString("X"));
            }
            return enString.ToString();
        }

        public static string EncryptString(string input)
        {
            if (input.Equals(string.Empty))
            {
                return input;
            }
            byte[] byKey = StringToByteArray("Qt3A!sia");
            byte[] IV = StringToByteArray("p*-W?m!s");
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = Encoding.UTF8.GetBytes(input);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            return Convert.ToBase64String(ms.ToArray());
        }

        private int GenerateSeed()
        {
            string skey = "Qt-Asia-" + this.SubKey;
            int x = 0x7d3;
            char[] ckey = skey.ToCharArray();
            for (int i = 0; i < ckey.Length; i++)
            {
                x += (ckey[i] * '\a') + 11;
            }
            return x;
        }

        public static string GetMacAddress(string macip)
        {
            StringBuilder strReturn = new StringBuilder();
            try
            {
                int remote = inet_addr(macip);
                long macinfo = 0;
                int length = 6;
                SendARP(remote, 0, ref macinfo, ref length);
                string temp = Convert.ToString(macinfo, 0x10).PadLeft(12, '0').ToUpper();
                int x = 12;
                for (int i = 0; i < 6; i++)
                {
                    if (i == 5)
                    {
                        strReturn.Append(temp.Substring(x - 2, 2));
                    }
                    else
                    {
                        strReturn.Append(temp.Substring(x - 2, 2) + ":");
                    }
                    x -= 2;
                }
                return strReturn.ToString().Replace(":", "");
            }
            catch
            {
                return "";
            }
        }

        [DllImport("Ws2_32.dll")]
        private static extern int inet_addr(string ipaddr);
        [DllImport("Iphlpapi.dll")]
        private static extern int SendARP(int DestIP, int SrcIP, ref long MacAddr, ref int PhyAddrLen);
        public static byte[] StringToByteArray(string value)
        {
            char[] values = value.ToCharArray();
            byte[] bytearr = new byte[values.Length];
            for (int i = 0; i < values.Length; i++)
            {
                bytearr[i] = Convert.ToByte(values[i]);
            }
            return bytearr;
        }
    }
}

